/**
 * 
 */
package buttons;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import util.commands.Command;
import util.resources.ResourceManager;

/**
 * Allows the user to use commands such as openning and closing files.
 * 
 * @author Josue Mirtil
 * 
 */
// TODO: ADD the resource file to make this work. Add menu "option".
public class MenuBar extends JMenuBar
{
    private Map<String, JMenu> menus;
    private static final long serialVersionUID = 1L;

    public MenuBar()
    {
        super();
        menus = new HashMap<String, JMenu>();
    }

    /**
     * Creates a new menu to place on the MenuBar
     */
    public void establishMenu(String menuname)
    {
        ResourceManager resources = ResourceManager.getInstance();
        resources.addResourcesFromFile("menu");
        menus.put(menuname, new JMenu(menuname));
        add(menus.get(menuname));
    }

    private abstract class MenuItemConstructor
    {
        public abstract JMenuItem createMenuItem(String label);
    }

    /**
	 * 
	 */
    public <T> JMenuItem addFileItems(String itemname, String menuname, boolean separator, final T target, final Command<T> command)
    {
        return addFileItemInternal(new MenuItemConstructor()
        {
            @Override
            public JMenuItem createMenuItem(String label)
            {
                return new JMenuItem(label);
            }
        }, itemname, menuname, separator, target, command);
    }

    /**
     * Takes a JMenuItem and converts it into a JRadioButton.
     */
    public <T> JMenuItem addRadioFileItems(String itemname, String menuname, boolean separator, final T target, final Command<T> command)
    {
        return addFileItemInternal(new MenuItemConstructor()
        {
            @Override
            public JMenuItem createMenuItem(String label)
            {
                return new JRadioButtonMenuItem(label);
            }
        }, itemname, menuname, separator, target, command);
    }

    /**
     * Adds a new JMenuItem for the given JMenu. Also, sets the command for the
     * new JMenuItem.
     */
    private <T> JMenuItem addFileItemInternal(MenuItemConstructor constructor, String itemname, String menuname, boolean separator, final T target, final Command<T> command)
    {
        ResourceManager resources = ResourceManager.getInstance();
        resources.addResourcesFromFile("menu");
        JMenuItem item = constructor.createMenuItem(itemname);
        item.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent ae)
            {
                command.execute(target);
            }
        });
        JMenu menu = menus.get(menuname);
        menu.add(item);
        if (separator)
        {
            menu.addSeparator();
        }

        return item;
    }

}
